package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.BizChatCoupons;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import io.jsonwebtoken.lang.Assert;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.mapper.BizChatCouponsMapper;
import com.ruoyi.business.domain.BizChatCoupons;
import com.ruoyi.business.service.IBizChatCouponsService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * chat优惠券Service业务层处理
 *
 * @author lxy
 * @date 2024-04-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BizChatCouponsServiceImpl extends ServiceImpl<BizChatCouponsMapper, BizChatCoupons> implements IBizChatCouponsService {

    @Resource
    private BizChatCouponsMapper bizBizChatCouponsMapper;

    /**
     * 查询chat优惠券列表
     *
     * @param bizBizChatCoupons chat优惠券
     * @return chat优惠券
     */
    @Override
    public List<BizChatCoupons> queryList(BizChatCoupons bizBizChatCoupons) {
        LambdaQueryWrapper<BizChatCoupons> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(bizBizChatCoupons.getName())) {
            queryWrapper.like(BizChatCoupons::getName, bizBizChatCoupons.getName());
        }
        if (StringUtils.isNotNull(bizBizChatCoupons.getType())) {
            queryWrapper.eq(BizChatCoupons::getType, bizBizChatCoupons.getType());
        }
        if (StringUtils.isNotNull(bizBizChatCoupons.getStartDate())) {
            queryWrapper.eq(BizChatCoupons::getStartDate, bizBizChatCoupons.getStartDate());
        }
        if (StringUtils.isNotNull(bizBizChatCoupons.getEndDate())) {
            queryWrapper.eq(BizChatCoupons::getEndDate, bizBizChatCoupons.getEndDate());
        }
        if (StringUtils.isNotEmpty(bizBizChatCoupons.getRedeemCode())) {
            queryWrapper.eq(BizChatCoupons::getRedeemCode, bizBizChatCoupons.getRedeemCode());
        }
        if (StringUtils.isNotNull(bizBizChatCoupons.getRedeemCount())) {
            queryWrapper.eq(BizChatCoupons::getRedeemCount, bizBizChatCoupons.getRedeemCount());
        }
        if (StringUtils.isNotEmpty(bizBizChatCoupons.getStatus())) {
            queryWrapper.eq(BizChatCoupons::getStatus, bizBizChatCoupons.getStatus());
        }
        if (StringUtils.isNotEmpty(bizBizChatCoupons.getImageUrl())) {
            queryWrapper.eq(BizChatCoupons::getImageUrl, bizBizChatCoupons.getImageUrl());
        }
        if (StringUtils.isNotNull(bizBizChatCoupons.getDiscountAmount())) {
            queryWrapper.eq(BizChatCoupons::getDiscountAmount, bizBizChatCoupons.getDiscountAmount());
        }
        if (StringUtils.isNotNull(bizBizChatCoupons.getDiscountRate())) {
            queryWrapper.eq(BizChatCoupons::getDiscountRate, bizBizChatCoupons.getDiscountRate());
        }
        if (StringUtils.isNotNull(bizBizChatCoupons.getMinPurchaseAmount())) {
            queryWrapper.eq(BizChatCoupons::getMinPurchaseAmount, bizBizChatCoupons.getMinPurchaseAmount());
        }
        return this.list(queryWrapper);
    }

    @Override
    public void createOrder(BizChatCoupons form) {
        // 验证订单表单的必填字段
        validateOrderForm(form);
        // 检查订单号是否唯一
        checkOrderNumberUnique(form);
        // 保存订单到数据库
        this.save(form);
    }


    /**
     * 验证订单表单的必填字段。
     * 使用了自定义的验证方法来简化重复的校验逻辑。
     *
     * @param form 订单表单对象
     */
    private void validateOrderForm(BizChatCoupons form) {
        validateNotEmpty(form.getImageUrl(), "图片地址不能为空");
        validateNotEmpty(form.getName(), "优惠券名称不能为空");
    }

    /**
     * 验证字符串类型字段不为空。
     *
     * @param value   字段值
     * @param message 如果字段为空时的错误信息
     */
    private void validateNotEmpty(String value, String message) {
        Assert.hasText(value, message);
    }

    /**
     * 验证对象类型字段不为null。
     *
     * @param value   字段值
     * @param message 如果字段为null时的错误信息
     */
    private void validateNotNull(Object value, String message) {
        Assert.notNull(value, message);
    }

    /**
     * 检查订单号的唯一性。
     * 如果订单号在数据库中已存在，并且不是当前订单（更新订单时），则抛出异常。
     *
     * @param form 订单表单对象
     */
    private void checkOrderNumberUnique(BizChatCoupons form) {
        Long id = form.getId();
        LambdaQueryWrapper<BizChatCoupons> queryWrapper = new LambdaQueryWrapper<>();
        // 设定查询条件为订单号相同
        queryWrapper.eq(BizChatCoupons::getDiscountAmount, form.getDiscountAmount());
        // 如果是更新订单，则排除当前订单的id
        if (id != null) {
            queryWrapper.ne(BizChatCoupons::getId, id);
        }
        // 如果查询结果大于0，说明订单号不唯一，抛出异常
        if (this.count(queryWrapper) > 0) {
            throw new ServiceException("订单号已存在");
        }
    }

}
